package com.ruoyi.aimodel.controller;

import com.ruoyi.aimodel.domain.AipptOutline;
import com.ruoyi.aimodel.service.DeepSeekService;
import com.ruoyi.aimodel.service.ZhipuService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.validation.Valid;
import java.io.IOException;
import java.util.UUID;
import java.util.logging.Logger;

@RestController
@RequestMapping("/api")
public class ZhiPuController {
    private static final Logger logger = Logger.getLogger(ZhiPuController.class.getName());
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(ZhiPuController.class);

    @Autowired
    private ZhipuService zhipuService;

    @PostMapping(value = "/getZhipuStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter getZhipuStream(@RequestBody AipptOutline aipptOutline) {
        SseEmitter emitter = new SseEmitter(180000L);
        if(aipptOutline.getUserid().isEmpty()){
            log.info("userid为空，临时会话");
            aipptOutline.setUserid(UUID.randomUUID().toString());
        }
        try {
            zhipuService.callZhipuStream(aipptOutline.getUserid(), aipptOutline.getContent())
                    .subscribe(
                            content -> {
                                try {
                                    emitter.send(content, MediaType.TEXT_PLAIN);
                                } catch (IOException e) {
                                    logger.severe("发送内容错误: " + e.getMessage());
                                    emitter.completeWithError(e);
                                }
                            },
                            error -> {
                                try {
                                    logger.severe("处理出现异常: " + error.getMessage());
                                    emitter.send(SseEmitter.event().data("Error: " + error.getMessage()));
                                } catch (IOException e) {
                                    logger.severe("发送了异常信息: " + e.getMessage());
                                    emitter.completeWithError(e);
                                } finally {
                                    emitter.complete();
                                }
                            },
                            () -> {
                                emitter.complete();
                            }
                    );
        } catch (Exception e) {
            try {
                logger.severe("Unexpected error: " + e.getMessage());
                emitter.send(SseEmitter.event().data("Error: " + e.getMessage()));
            } catch (IOException ex) {
                logger.severe("Error sending unexpected error message: " + ex.getMessage());
                emitter.completeWithError(ex);
            } finally {
                emitter.complete();
            }
        }

        return emitter;
    }

    @PostMapping("/getZhipuChat")
    public String getZhipuChat(@RequestBody AipptOutline aipptOutline) {
        if(aipptOutline.getUserid().isEmpty()){
            log.info("userid为空，临时会话");
            aipptOutline.setUserid(UUID.randomUUID().toString());
        }
        String resultMsg=zhipuService.callZhipu(aipptOutline.getUserid(), aipptOutline.getContent());
        log.info(resultMsg);
        return resultMsg;
    }




}